package com.sandwich.admin.service.impl;

import com.alibaba.fastjson.JSON;
import com.sandwich.admin.apibean.ContentSummaryJson;
import com.sandwich.admin.apibean.ContentSummaryRequest;
import com.sandwich.admin.domain.CategoryContentSummaryEntity;
import com.sandwich.admin.domain.ContentSummaryEntity;
import com.sandwich.admin.domain.repository.CategoryContentSummaryRepository;
import com.sandwich.admin.domain.repository.ContentSummaryRepository;
import com.sandwich.admin.service.ContentSummaryService;
import com.sandwich.api.apibean.v1.ContentRequest;
import com.sandwich.common.Commons;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

@Service
public class ContentSummaryServiceImpl implements ContentSummaryService {

    @Autowired
    private ContentSummaryRepository contentSummaryRepository;
    @Autowired
    private CategoryContentSummaryRepository categoryContentSummaryRepository;

    @Override
    public Page<ContentSummaryEntity> findCategorySummaryEntityPage(ContentRequest contentRequest) {
        long categoryPk = contentRequest.getCategoryPk();
        PageRequest pageRequest = PageRequest.of(contentRequest.getCurrentPage() - 1, contentRequest.getPageSize());
        Page<ContentSummaryEntity> pageResult = categoryPk > 0 ? contentSummaryRepository.findPageCategoryByCategoryPk(categoryPk, pageRequest)
                : contentSummaryRepository.findAll(pageRequest);
        return pageResult;
    }


    @Override
    @Transactional
    public ContentSummaryEntity addOrUpdateContentSummary(ContentSummaryRequest contentSummaryReq) {
        long pk = contentSummaryReq.getPk();
        long categoryPk = contentSummaryReq.getCategoryPk();
        ContentSummaryEntity entity = contentSummaryRepository.findById(pk).orElse(null);

        ContentSummaryEntity saveEntity = null;
        if (Objects.isNull(entity)) {
            ContentSummaryEntity newEntity = new ContentSummaryEntity();
            newEntity.setContentJson(JSON.toJSONString(new ContentSummaryJson(contentSummaryReq)));
            newEntity.setPicture(contentSummaryReq.getPictures());
            newEntity.setVideoUrl(contentSummaryReq.getVideoUrl());
            saveEntity = contentSummaryRepository.save(newEntity);
        } else {
            entity.setContentJson(JSON.toJSONString(contentSummaryReq));
            entity.setPicture(contentSummaryReq.getPictures());
            entity.setVideoUrl(contentSummaryReq.getVideoUrl());
            saveEntity = contentSummaryRepository.save(entity);
        }

        //增加关联关系
        CategoryContentSummaryEntity categoryContentSummaryEntity = categoryContentSummaryRepository.findFirstByCategoryPkAndContentSummaryPk(categoryPk, saveEntity.getPk());
        if (Objects.isNull(categoryContentSummaryEntity)) {
            categoryContentSummaryEntity = new CategoryContentSummaryEntity();
            categoryContentSummaryEntity.setContentSummaryPk(saveEntity.getPk());
            categoryContentSummaryEntity.setCategoryPk(categoryPk);
        } else {
            categoryContentSummaryEntity.setStatus(Commons.ENABLE_STATUS);
        }
        categoryContentSummaryRepository.save(categoryContentSummaryEntity);

        return saveEntity;
    }
}
